/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package aiaudio.processing;

import aiaudio.lastfm.hbase.CannotCreateTableException;
import aiaudio.processing.base.AlgorithmParameters;
import aiaudio.processing.base.TimeMeasuredAlgorithm;
import aiaudio.processing.network.NetworkCalculationAlgorithm;
import aiaudio.processing.prediction.MakePredictionAlgorithm;
import aiaudio.processing.ratings.PreprocessRatingsAlgorithm;
import aiaudio.processing.reduce.KMeansDimensionReducingAlgorithm;
import aiaudio.processing.split.DataSetSplittingAlgorithm;
import java.io.IOException;

/**
 *
 * @author nastya
 */
public class ProcessingAlgorithm extends TimeMeasuredAlgorithm<ProcessingAlgorithmParameters> {

    public ProcessingAlgorithm(MainTableGroup mainTableGroup) {
        super(mainTableGroup, "full algorithm");
    }

    @Override
    protected void removeTemporaryTables() {
    }

    @Override
    protected void createTemporaryTables() throws IOException {
    }

    @Override
    protected void process() throws IOException, CannotCreateTableException, InterruptedException, ClassNotFoundException {
        preprocessRatings();
        splitData();
        reduceArtistsCount();
        calculateNeighbours();
        predictTopList();
    }

    private void predictTopList() throws ClassNotFoundException, InterruptedException, CannotCreateTableException, IOException {
        MakePredictionAlgorithm makePredictionAlgorithm = new MakePredictionAlgorithm(mainTableGroup);
        makePredictionAlgorithm.setParameters(parameters.getPredictionParameters());
        makePredictionAlgorithm.start();
    }

    private void calculateNeighbours() throws CannotCreateTableException, IOException, ClassNotFoundException, InterruptedException {
        NetworkCalculationAlgorithm alg = new NetworkCalculationAlgorithm(mainTableGroup);
        alg.setParameters(parameters.getNetworkParameters());
        alg.start();
    }

    private void reduceArtistsCount() throws InterruptedException, IOException, CannotCreateTableException, ClassNotFoundException {
        KMeansDimensionReducingAlgorithm algorithm = new KMeansDimensionReducingAlgorithm(mainTableGroup);
        algorithm.setParameters(parameters.getReducingParameters());
        algorithm.start();
    }

    private void splitData() throws CannotCreateTableException, InterruptedException, IOException, ClassNotFoundException {
        DataSetSplittingAlgorithm dataSetSplittingAlgorithm = new DataSetSplittingAlgorithm(mainTableGroup);
        dataSetSplittingAlgorithm.setParameters(parameters.getSplitParameters());
        dataSetSplittingAlgorithm.start();
    }

    private void preprocessRatings() throws InterruptedException, CannotCreateTableException, IOException, ClassNotFoundException {
        PreprocessRatingsAlgorithm preprocessRatingsAlgorithm = new PreprocessRatingsAlgorithm(mainTableGroup);
        preprocessRatingsAlgorithm.setParameters(parameters.getRatingParameters());
        preprocessRatingsAlgorithm.start();
    }

    @Override
    protected void measureAll() throws IOException {
    }

    @Override
    protected void writeParameters(ProcessingAlgorithmParameters parameters) throws IOException {
    }
}
